﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Service;
using System.IO;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class AttentionHelper
    {
        CKCDB mydb = new CKCDB();
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Attention> FindAll()
        {
            var apps = from n in mydb.Attentions
                       orderby n.AddTime descending
                       select n;
            return apps;
        }
        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Attention FindById(int id)
        {
            var app = mydb.Attentions.Find(id);
            return app;
        }

        /// <summary>
        /// FindByUserName(string user)
        /// </summary>
        /// <param name="user">用户名</param>
        /// <returns></returns>
        public IEnumerable<Attention> FindByUserName(string user)
        {
            var apps = from n in mydb.Attentions
                       where n.UserName == user
                       orderby n.AddTime descending
                       select n;
            return apps;
        }

        /// <summary>
        ///  FindByShapeCodeAndType(string code, int type) 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="type">0-Topic;1-Trouble;2-Answer;3-Tribune;4-TribuneTopic;5-Knowledge;6-Document;7-Commodity</param>
        /// <returns></returns>
        public IEnumerable<Attention> FindByShapeCodeAndType(string code, int type)
        {
            var app = from a in mydb.Attentions
                      where a.ShapeCode == code && a.BelongTo == type
                      orderby a.AddTime descending
                      select a;
            return app;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Attention</param>
        /// <returns></returns>
        public bool Create(Attention model)
        {
            try
            {
                mydb.Attentions.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Create in table<Attentions>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Attention</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            try
            {
                mydb.Attentions.Remove(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<Attentions>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }
    }
}